var table 
$(document).ready(function() {
	
	var url=sy.bp()+"/winetype/findwinetype.action";
var option ={
		"aLengthMenu":[2,10,15,20], //动态指定分页后每页显示的记录数。
		 "sPaginationType": "full_numbers",
		 "serverSide":true,
		"searching":false,//禁用搜索
		"lengthChange":false, //是否启用改变每页显示多少条数据的控件
		"sort" : "position",  //是否开启列排序，对单独列的设置在每一列的bSortable选项中指定
		"deferRender":true,//延迟渲染
		"bStateSave" : false, //在第三页刷新页面，会自动到第一页
		"iDisplayLength" : 2,  //默认每页显示多少条记录
		"iDisplayStart" : 0,
		"ordering": false,//全局禁用排序
		"dom": '<l<\'#topPlugin\'>f>rt<ip><"clear">',
        "sAjaxSource":url,
		"aoColumns" : [{  //aoColumns设置列时，不可以任意指定列，必须列出所有列。
			"mData" : "id",
			"orderable": false , // 禁用排序
			"sDefaultContent" : "",
			"sWidth" : "2%"
        },{
			"mData" : "id",
			"orderable" : false, // 禁用排序
			"sDefaultContent" : "",
			"sWidth" : "6%",
			
		}, {
			"mData" : "name",
			"orderable" : false, // 禁用排序
			"sDefaultContent" : "",
			"sWidth" : "6%",
		}, {
			"mData" : "typedetail",
			"orderable" : false, // 禁用排序
			"sDefaultContent" : "",
			"sWidth" : "6%"
		}, {
			"mData" : "pid",
			"orderable" : false, // 禁用排序
			"sDefaultContent" : "",
			"sWidth" : "6%"
		},{
			"mData" : "id",
			"orderable" : false, // 禁用排序
			"sDefaultContent" : '',
			"sWidth" : "10%",
		    "render":function(data, type, full, meta){  //render改变该列样式,4个参数，其中参数数量是可变的。
		    	  var topPlugin1=' <button id ="update"  class="btn btn-primary" data-id='+data+'>修改</button>' ;
				    return	data='<button id="deleteOne" class="btn btn-danger btn-sm" data-id='+data+'>删 除</button>'+topPlugin1;
		    }}
		],
		"columnDefs" : 
		[{
			"orderable" : false, // 禁用排序
			"targets" : [0], // 指定的列
			"data" : "id",
			"render" : function(data, type, full, meta) {
				  
				return '<input type="checkbox" value="'+ data + '" name="id"/>';
			}
		}],
		"oLanguage" : { // 国际化配置
			"sProcessing" : "正在获取数据，请稍后...",
			"sLengthMenu" : "显示 _MENU_ 条",
			"sZeroRecords" : "没有找到数据",
			"sInfo" : "从 _START_ 到  _END_ 条记录 总记录数为 _TOTAL_ 条",
			"sInfoEmpty" : "记录数为0",
			"sInfoFiltered" : "(全部记录数 _MAX_ 条)",
			"sInfoPostFix" : "",
			"sSearch" : "搜索",
			"sUrl" : "",
			"oPaginate" : {
				"sFirst" : "第一页",
				"sPrevious" : "上一页",
				"sNext" : "下一页",
				"sLast" : "最后一页"
			}
		},
		drawCallback: function( settings ) {
	        $('input[name=allChecked]')[0].checked=false;//取消全选状态
	    }
		
}
	table= $("#example1").DataTable(option);
});



function winetypeadd() {
	// bootstrap的弹框
	$('#wineadd').modal('show');

}

function wineaddtj() {
	var url = sy.bp() + "/winetype/wineadd.action";
	$.ajax({
		url :url,
		type:"post",
		data:{
			"id":$("#winetypeid").val(),
			"name":$("#winename").val(),
			"typedetail":$("#winetype").val(),
			"pid":$("#winepid").val(),
		},
		dataType : "json",
		success : function(data) {
			console.info(data);
			if(data.num==1){
				alert("保存成功");
			}else{
				alert("保存失败");
			}
			table.ajax.reload();
			//window.location.reload();
			$('#wineadd').modal('hide');
		}
	}); 
}

//修改
$(document).delegate('#update','click',function(){
	var id=$(this).data("id");
	//alert(id);
	var url = sy.bp()+"/winetype/selectByPrimaryKey.action?id="+id;
	$.ajax({
		url:url,
		type:"get",
		success:function(data){
		
			$("#wineid1").val(data.se.id),
			$("#winename1").val(data.se.name),
			$("#winetype1").val(data.se.typedetail),
			$("#winepid1").val(data.se.pid),
			$('#wineupdate').modal('show')
		}
	});
});

function wineupdatetj(){
	var url = sy.bp()+"/winetype/wineupdate.action";
	var id =$("#wineid1").val();
	var name =$("#winename1").val();
	var typedetail =$("#winetype1").val();
	var pid =$("#winepid1").val();
	$.ajax({
		url:url,
		type:"post",
		data:{
			"id":id,
			"name":name,
			"typedetail":typedetail,
			"pid":pid,
		},
		dataType:"json",
		success:function(data){
			console.info(data);
			if(data.num == 1){
				alert("修改成功！");
			}else{
				alert("修改失败！");
			}
			table.ajax.reload();
			$('#wineupdate').modal('hide');	
		}
	});
}

/**
 * 单个删除
 * 
 * @param id
 */
$(document).delegate('#deleteOne', 'click', function() {
	var id = $(this).data("id");
	// alert(id);
	$('#winedelete').modal('show');
	$("#onedeleteid").val(id);
});
function winedeltetj() {
	var id = $("#onedeleteid").val();
	// alert(id);
	var url = sy.bp() + "/winetype/winedelete.action?id=" + id;
	$.ajax({
		url : url,
		type : "get",
		dataType : "json",
		cache : false,
		success : function(data) {
			if (data.num == 1) {
				alert("删除成功！");
				
			} else {
				alert("删除失败！");
			}
			table.ajax.reload();
			$("#onedeleteid").modal('hide');
		}
	});
}

//批量删除
function deleteAll() {
	var theArray = [];
	$("input[name=id]:checked").each(function() {
		theArray.push($(this).val());
	});
	var ids = theArray.toString();
	if (ids == null || ids == "") {
		alert("请至少选择一个");
	} else {
		// alert(ids);
		$.ajax({
			url : sy.bp() + "/winetype/winedeletes.action",
			type : "post",
			data : {
				"ids" : ids,
			},
			dataType : "json",
			success : function(data) {
				if (data.flag == 1) {
					alert("删除成功！");
				} else {
					alert("删除失败！");
				}
				table.ajax.reload();
				$('#deleteAll').modal('hide');
			}
		});

	}
}
